#include "dbrecord.h"
#include <QString>
#include <QIcon>
#include <QTime>
#include <QContact>

#include "settings.h"

QTM_USE_NAMESPACE

DBRecord::DBRecord( QObject * parent ): m_contact( 0 ),	m_id( -1 ),
			m_avatar( QIcon::fromTheme( "general_default_avatar", QIcon( IMG_AVATAR ) ) ),
			QObject( parent )
{
}

DBRecord::~DBRecord()
{
	// This gives "glibc detected *** free(): invalid pointer" errors if the object is created
	// somewhere outside this class
	delete m_contact;
}
void DBRecord::setContact( QContact const & c )
{
	m_contact = new QContact;
	* m_contact = c;
}

QString DBRecord::callDuration() const
{
	QString str;
	QTime t;
	t.setHMS(0, 0, 0);
	if ( ! m_endTime.toTime_t() == 0 )
	{
		uint delta = m_endTime.toTime_t() - m_startTime.toTime_t();
		uint hours = delta / 3600;
		delta = delta % 3600;
		uint mins = delta / 60;
		uint secs = delta % 60;
		t.setHMS( hours, mins, secs );
	}
	QString s1;
	if ( t.hour() > 0 )
	{
		s1.setNum( t.hour() );
		str.append( s1 );
		str.append( tr( "h " ) );
	}
	if ( t.minute() > 0 )
	{
		s1.setNum( t.minute() );
		str.append( s1 );
		str.append( tr( "m " ) );
	}
		s1.setNum( t.second() );
		str.append( s1 );
		str.append( tr( "s" ) );
	return str;
}

const QContact * DBRecord::contact() const
{
//	if ( ! m_contact )
//		emit contactRequested( m_id );
	return m_contact;
}

QIcon DBRecord::avatar() const
{
//	if ( ! m_contact )
//		emit contactRequested( m_id );
	return m_avatar;
}
